"""
审计日志 Request VO
"""

import utils.enum_utils as enum
from models import (
    ListRequestBase,
    QueryFieldBase,
    RequestBase,
)
from my_typing import *
from utils.data_utils.special import ObjectIdStr
from utils.validator_utils.pydantic_validator.field import (
    PathCharField,
    QueryCharField,
    QueryIntegerField,
    QueryNormalField,
)


class AuditLogPathField:
    """ 审计日志 | 路径 | 字段 """

    audit_log_id: ObjectIdStr = PathCharField(..., title="审计日志 ID")


class AuditLogDetailRequest(RequestBase):
    """ 审计日志 | 详情 | 请求 """

    audit_log_id: ObjectIdStr = AuditLogPathField.audit_log_id


class AuditLogListRequest(ListRequestBase):
    """ 审计日志 | 列表 | 请求 """

    # -------------------- 排序字段 --------------------
    sort_by_ip: enum.MongoSortOrNone = QueryCharField(None, title="根据 IP 排序")
    sort_by_model: enum.MongoSortOrNone = QueryCharField(None, title="根据模型排序")
    sort_by_operation: enum.MongoSortOrNone = QueryCharField(None, title="根据操作类型排序")
    sort_by_request_time: enum.MongoSortOrNone = QueryCharField(None, title="根据日志访问时间排序")
    sort_by_user_id: enum.MongoSortOrNone = QueryCharField(None, title="根据用户 ID 排序")

    # -------------------- 查询字段 --------------------
    ip: StrOrNone = QueryCharField(None, title="IP 地址")
    method: enum.Method | None = QueryCharField(None, title="请求方法")
    operation: enum.ModelOperation = QueryCharField(None, title="操作类型")
    po_id: ObjectIdStr | None = QueryCharField(None, title="PO ID")
    request_id: StrOrNone = QueryCharField(None, title="请求 ID")
    exception: BoolOrNone = QueryNormalField(None, title="是否发生异常")
    status_code: enum.HTTPStatusCode | None = QueryIntegerField(None, title="响应状态码")
    request_time_end: DateTimeOrNone = QueryCharField(None, title="日志访问结束时间")
    request_time_start: DateTimeOrNone = QueryCharField(None, title="日志访问开始时间")
    user_id: ObjectIdStr | None = QueryCharField(None, title="用户 ID")

    @classmethod
    def from_query(
        cls,
        page: int = QueryFieldBase.page,
        page_size: int = QueryFieldBase.page_size,
        sort_by_ip: enum.MongoSortOrNone = QueryCharField(None, title="根据 IP 排序"),
        sort_by_model: enum.MongoSortOrNone = QueryCharField(None, title="根据模型排序"),
        sort_by_operation: enum.MongoSortOrNone = QueryCharField(None, title="根据操作类型排序"),
        sort_by_request_time: enum.MongoSortOrNone = QueryCharField(None, title="根据日志访问时间排序"),
        sort_by_user_id: enum.MongoSortOrNone = QueryCharField(None, title="根据用户 ID 排序"),
        ip: StrOrNone = QueryCharField(None, title="IP 地址"),
        method: enum.Method | None = QueryCharField(None, title="请求方法"),
        operation: enum.ModelOperation = QueryCharField(None, title="操作类型"),
        po_id: ObjectIdStr | None = QueryCharField(None, title="PO ID"),
        request_id: StrOrNone = QueryCharField(None, title="请求 ID"),
        exception: BoolOrNone = QueryNormalField(None, title="是否发生异常"),
        status_code: enum.HTTPStatusCode | None = QueryIntegerField(None, title="响应状态码"),
        request_time_end: DateTimeOrNone = QueryCharField(None, title="日志访问结束时间"),
        request_time_start: DateTimeOrNone = QueryCharField(None, title="日志访问开始时间"),
        user_id: ObjectIdStr | None = QueryCharField(None, title="用户 ID"),
    ) -> "AuditLogListRequest":
        return cls(
            page=page,
            page_size=page_size,
            sort_by_ip=sort_by_ip,
            sort_by_model=sort_by_model,
            sort_by_operation=sort_by_operation,
            sort_by_request_time=sort_by_request_time,
            sort_by_user_id=sort_by_user_id,
            ip=ip,
            method=method,
            operation=operation,
            po_id=po_id,
            request_id=request_id,
            exception=exception,
            status_code=status_code,
            request_time_end=request_time_end,
            request_time_start=request_time_start,
            user_id=user_id,
        )
